import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28956
 * Date: 2024-07-19
 * Time: 17:32
 */
public class MyStack implements IStack{
    private int[] elem;
    private int usedSize;//可以计数以及存数据元素下标
    private static final int DEFAULT_CAPACITY = 10;

    public MyStack() {
        elem = new int[DEFAULT_CAPACITY];
    }
    @Override
    public void push(int x) {
        if(full()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = x;
        usedSize++;
    }

    @Override
    public boolean full() {
        if(usedSize == elem.length){
            return true;
        }
        return false;
    }

    @Override
    public int pop() {
        if(empty()){
            throw new EmptyException("栈空了!");
        }
        int old = elem[usedSize-1];
        usedSize--;
        return old;
    }

    @Override
    public int peek() {
        if(empty()){
            throw new EmptyException("栈空了!");
        }
       return elem[usedSize-1];

    }

    @Override
    public boolean empty() {
        return usedSize == 0;
    }
}
